package com.brillilab.starter.controller.aio.hardware;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.po.hardware.HardWareOperateRecord;
import com.brillilab.domain.po.lab.LabDevice;
import com.brillilab.domain.po.space.Device;
import com.brillilab.domain.vo.hardware.HardWareOperateRecordVo;
import com.brillilab.domain.vo.space.DeviceLocker;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.hardware.IHardwareOperateRecordService;
import com.brillilab.service.core.lab.ILabDeviceService;
import com.brillilab.service.core.space.IDeviceService;
import com.brillilab.service.logic.space.DeviceLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wuzhiyu
 * @Title: HardwareController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/1/2910:49
 */
@RestController
@RequestMapping("/api/hardware")
public class HardwareController {
    @Resource
    private IHardwareOperateRecordService hardwareOperateRecordService;
    @Resource
    private IDeviceService deviceService;
    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ILabDeviceService labDeviceService;
    @Resource
    private DeviceLogic deviceLogic;

    @AspectLog
    @PostMapping("/update")
    public ResponseVo update(@RequestBody HardWareOperateRecordVo record, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.notNull(record.getDeviceIdList(), "device id is null");
        Device device = deviceService.get(record.getDeviceIdList().get(0));
        Assert.notNull(device.getAioDeviceId(), "未绑定一体机");
        Assert.notNull(device, "设备不存在");
        UserInfoVo user = usersLogic.getUserByToken(token);
        Assert.isTrue(user.getLabId().equals(device.getLabId()), "设备不存在");
        record.setAioId(device.getAioDeviceId());
        Assert.isTrue((!StringUtils.isEmpty(record.getCamAccount())) || (!StringUtils.isEmpty(record.getLockerId())), "param error");
        return hardwareOperateRecordService.updateRecord(record) ? ResponseVo.success() : ResponseVo.failure();
    }

    @GetMapping("/list")
    public ResponseVo list(String serialNo, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.notNull(serialNo, "serialNo is null");
        LabDevice labDevice = labDeviceService.selectByNo(serialNo);
        Assert.notNull(labDevice, "不存在的一体机");
        UserInfoVo user = usersLogic.getUserByToken(token);
        Assert.isTrue(labDevice.getLabId().equals(user.getLabId()), "不存在的一体机");
        List<HardWareOperateRecord> list = hardwareOperateRecordService.list(labDevice.getDeviceId());
        return ResponseVo.success(list);
    }

}
